/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen;

import java.io.PrintStream;

/** Evaluate a Java expression.
 * <p>
 * This expression type allows you to include Java code of your specification in the generated configurator.
 * It is expected that the specified Java code will be a Java expression, as opposed to a string of arbitrary Java syntax, but if you don't mind living dangerously,
 *   you might be able to get away with other syntactic forms, since the code is (currently) copied to the output mostly unmodified.
 * </p>
 * <p>
 * A {@code Code} expression can be optionally assigned an id.
 * If you just want to refer to a simple, side-effect-free Java expression, {@link NoFX} may be more appropriate.
 * </p>
 */
public final class Code extends CoreExpr
{
	private final int line;
	private final String type;
	private final String code;

	/** Construct a {@code Code}.
	 * @param c The {@link Config} that will contain this expression.
	 * @param l The line number this expression was on.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param k The Java code to evaluate.
	 */
	public Code(Config c,int l,String t,String i,String k) {super(c,i);line = l;type = t;code = k;}

	void printType(PrintStream out) throws InvalidConfigException
	{
		if (type == null) needType(line,code);
		out.print(type);
	}

	void printCore(PrintStream out) {out.print(code);}
}
